package model

import (
	"choujiang/common/db"
)

type Region struct {
	Id         int64  `xorm:"INT(11) not null pk autoincr"`
	ParentId   int64  `xorm:"INT(11) not null default 0"`
	RegionName string `xorm:"VARCHAR(50) not null"`
	RegionType int64  `xorm:"INT(11) not null default 0"`
	RegionArea int64  `xorm:"INT(11) not null default 0"`
}

func GetRegionById(id int64) *Region {
	region := new(Region)
	has, err := db.MysqlEngine.Where("id=?", id).Get(region)
	if !has || err != nil {
		return nil
	}
	return region
}

func ParseRegionName(name string, regionType int64) (id int64) {
	region := new(Region)
	if has, err := db.MysqlEngine.Where("region_name LIKE ? AND region_type=?", "%"+name+"%", regionType).Get(region); !has || err != nil {
		id = 0
		return
	}
	id = region.Id
	return
}

func GetProvince(id int64) string {
	region := new(Region)
	has, err := db.MysqlEngine.Where("id=? AND region_type=?", id, 1).Get(region)
	if !has || err != nil {
		return "未设置"
	}
	return region.RegionName
}

func GetCity(id int64) string {
	region := new(Region)
	has, err := db.MysqlEngine.Where("id=? AND region_type=?", id, 2).Get(region)
	if !has || err != nil {
		return "未设置"
	}
	return region.RegionName
}
